package com.phiture.erp.produce.common.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Getter
@AllArgsConstructor
public enum MrpCalculationTypeEnum {
    PLANNING_DOCUMENTS(1, "plan_documents_parts", "规划件"),
    PHANTOM(2, "phantom_parts", "虚拟件"),
    SELF_MADE_PARTS(3, "self_made_parts", "自制件"),
    PURCHASED_PARTS(4, "purchased_parts", "外购件"),
    SUBCONTRACTED_ITEM(5, "subcontracted_item_parts", "委外件"),
    OTHER(6, "other_parts", "其他");

    private final Integer type;
    private final String code;
    private final String description;

    private static final Map<Integer, MrpCalculationTypeEnum> TYPE_MAP = Arrays.stream(values())
            .collect(Collectors.toMap(MrpCalculationTypeEnum::getType, obj -> obj));

    public static MrpCalculationTypeEnum fromType(Integer type) {
        Assert.notNull(type, "MRP计算类型不能为空");
        MrpCalculationTypeEnum typeEnum = TYPE_MAP.get(type);
        if (typeEnum == null) {
            log.error("[MrpCalculationTypeEnum] 无效的MRP计算类型: {}", type);
            throw new IllegalArgumentException("无效的MRP计算类型: " + type);
        }
        return typeEnum;
    }
}